<?php
namespace jy_friends_shop;
defined('BY_JYA') or exit('error');
/*
	代理佣金
	加价代理模式是否也是通过监听付款事件来获取佣金？
	不好，应该在下单时。这样，在下单时接收到电商客发给活动插件的事件时，即在本插件创建相应订单备用。执行下面的create方法。执行create方法时，从会话中获取当前店家ID
	有一个问题，代理商查看自己的订单时，该当如何查看？
	查commission表！
*/
class commission extends \plugin{
	/*
		生成佣金，生成思路。从会话中读取代理商ID，结合商品，寻找上家。如果上家是总店，则只计算一份佣金。否则一直往上追溯，直到找到为止
		生成佣金是否支持订单内有多个商品呢？暂不支持。
		计算佣金最麻烦的地方就是，扣除优惠的部分。这个暂时不管
	*/
	function create($params){
		$open=intval($this->setting['open']);
		if($open==0){
			return;
		}
		$goods_id=$params['goods_id'];
		$goods=pdo_get('jy_weishop_goods',array('id'=>$goods_id));
		$commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid'],
			'ordersn'=>$params['ordersn'],
			'order_price'=>$params['order_price'],
			'commission_money'=>($params['price']-$goods['price'])*$params['num'],
			'createtime'=>TIMESTAMP,
		);
		//生成代理订单
		$this->insert('order',$commission_order);
		$new_commission=array(
			'uniacid'=>$_SESSION['uniacid'],
			'ordersn'=>$params['ordersn'],
			'goodsid'=>$goods_id,
			'createtime'=>TIMESTAMP,
		);
		$agent_goods=$this->get('goods',array('mid'=>$this->session('agent_id'),'goodsid'=>$goods_id));
		$continue=true;
		do{
			$new_commission['mid']=$agent_goods['mid'];
			if($agent_goods['from_mid']){
				$parent_agent_goods=$this->get('goods',array('mid'=>$agent_goods['from_mid'],'goodsid'=>$goods_id));
				$new_commission['money']=$params['num']*($agent_goods['price']-$parent_agent_goods['price']);
			}else{
				$new_commission['money']=$params['num']*($agent_goods['price']-$goods['price']);
				$continue=false;
			}
			$this->insert('commission',$new_commission);
			$agent_goods=$parent_agent_goods;
		}while($continue);
		return true;
	}
	/*
		订单完成时，把佣金状态由初始的订单待完成升级为待结算
	*/
	public function order_complete($order){
		$this->update('commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('ordersn'=>$order['ordersn']));
	}
	/*
		创建结算单
	*/
	public function create_balance($mid){
		if(!$mid){
			exi('请先选择要结算的会员！','error');
		}
		//获取可结算佣金
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'status'=>1);
		if($this->setting['balance_day']){
			//结算天数
			$time=TIMESTAMP-intval($this->setting['balance_day'])*86400;
			$params['complete_time <']=$time;
		}
		$commissions=$this->getall('commission',$params);
		if(!$commissions){
			exi('没有可结算的佣金！','error');
		}
		$commission_num=count($commissions);
		$money=0;
		foreach($commissions as $commission){
			$money+=$commission['money'];
		}
		//结算最低佣金检测
		if($money<$this->setting['balance_money']){
			exi('可结算佣金太少，无法结算！','error');
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'commission_num'=>$commission_num,
			'money'=>$money,
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		//结算手续费
		if($this->setting['balance_per']){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		foreach($commissions as $commission){
			$update=array(
				'status'=>2,
				'balancesn'=>$new_balance['balancesn'],
			);
			$this->update('commission',$update,array('id'=>$commission['id']));
		}
		return $balance_id;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		$this->update('balance',$update,array('id'=>$balance_id));
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type){
		$balance=$this->get('balance',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['final_money'],$balance['balancesn']);
		$update=array(
			'status'=>3,
			'pay_type'=>$pay_type,
			'pay_trade_no'=>$trade_no,
			'pay_time'=>TIMESTAMP,
		);
		$this->update('balance',$update,array('id'=>$balance['id']));
		return true;
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,$this->plugin_name,$remark='分销佣金打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn){
		$params=array(
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'remark'=>'代理商佣金发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		return $res['payment_no'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn){
		exi('暂不支持打款到银行账户！','error');
	}
}
	